<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/11 0011
 * Time: 13:52
 */

namespace app\index\controller;


use think\cache\driver\Redis;
use think\Controller;
use think\facade\Cookie;

class Book extends Controller
{
    //书籍详情
    public function bookInfo(){
        $m_id = session('member_id');
        if(!empty($m_id)){
            $member = db('member')->cache(true,60)->where('id',$m_id)->find();
            $this->assign([
                'member' => $member
            ]);
        }
        $book_id = input("book_id");
//        dump($res);die;
        if(empty($book_id)) return "";
        $book = db("book")->where('state',1)->where("id",$book_id)->cache(60)->find();
        $this->book_pv($book_id,$book);
        $exceptional = $this->exceptional($book_id);
        $recommend = $this->recommend($book_id);
//        书籍推荐
        $bookRecommend = $this->bookRecommend($book);
//        作者信息
        $authorInfo = db("member")->where('id',$book['m_id'])->find();
//        站点推荐
        $siteRecommend = $this->siteRecommend();
//        作者推荐
        $authorBook = $this->authorBook($book);
        $footer = $this->footer();
        $newChapterMax = db("chapter")->where('book_id',$book_id)->max('id');
        $newChapter = db("chapter")->where('id',$newChapterMax)->find();
//        dump($newChapter);die;
//        return $book_id;
        $this->assign([
            'book' => $book,
            'exceptional' => $exceptional,
            'recommend' => $recommend,
            'bookRecommend' => $bookRecommend,
            'authorInfo' => $authorInfo,
            'siteRecommend' => $siteRecommend,
            'authorBook' => $authorBook,
            'footer' => $footer,
            'newChapter' => $newChapter,
            'current' => request()->controller()
        ]);
        return view("book_info");
    }
//    打赏数据
    public function exceptional($book_id){
//        $m_id = session('member_id');
        $exceptional = db("exceptional")
            ->where('book_id',$book_id)
//            ->where('member_id',$m_id)
            ->column('num');
        $num = 0;
        for ($i = 0; $i<count($exceptional);$i++){
            $num+=$exceptional[$i];
        }
        return $num;
    }
//    打赏逻辑
    public function except(){
        if(request()->post()){
            $m_id = session('member_id');
            $book_id = request()->param('book_id');
            if(empty($m_id)){
                return ['state' => -1,'msg' => '请先登录'];
            }
            $member = db("member")->where("id",$m_id)->find();
            if($member['money'] < 5){
                return ['state' => -1,'msg' => '金币不足请先充值'];
            }
            db("member")->where("id",$m_id)->setDec('money', 5);
            $data = db("exceptional")
                ->where('book_id',$book_id)
                ->where('member_id',$m_id)
                ->find();
            if(empty($data)){
                $param = [
                    'book_id' => $book_id,
                    'member_id' => $m_id,
                    'num' => 5,
                    'current_time' => time(),
                ];
                $res = db('exceptional')->insert($param);
                if(!$res) return ['state' => -2,'msg' => '系统错误'];
                $exceptional = $this->exceptional($book_id);
                return ['state' => 200,'msg' => '打赏成功','exceptional' =>$exceptional];
            }
            $res = db("exceptional")
                ->where('book_id',$book_id)
                ->where('member_id',$m_id)
                ->setInc('num',5);
            if(!$res) return ['state' => -2,'msg' => '系统错误'];
            $exceptional = $this->exceptional($book_id);
            return ['state' => 200,'msg' => '打赏成功','exceptional' =>$exceptional];
        }
    }
//    推荐数据
    public function recommend($book_id){
        $recommend = db("recommend")
            ->where('book_id',$book_id)
            ->count();
        return $recommend;
    }

//    推荐逻辑
    public function recom(){
        if(request()->post()){
            $m_id = session('member_id');
            $book_id = request()->param('book_id');
            if(empty($m_id)){
                return ['state' => -1,'msg' => '请先登录'];
            }
            //查看该用户是否投过票
            $recommend = db("recommend")
                ->where('book_id',$book_id)
                ->where('member_id',$m_id)
                ->find();
            if(empty($recommend)){
                $data = [
                    'member_id' => $m_id,
                    'book_id' => $book_id,
                    'num' => 1,
                    'current' => time()
                ];
                $res = db("recommend")->insert($data);
                if(!$res) return ['state' => -2,'msg' => '系统错误'];
                return ['state' => 200,'msg' => '投票成功','rem' => $this->recommend($book_id)];
            }else{
                return ['state' => -3,'msg' => '请勿重复投票!!'];
            }

        }
    }

//    书籍推荐 (5本)
    public function bookRecommend($book){
        $bookRecommend = db("book")
            ->where('cate_id',$book['cate_id'])
            ->where('state',1)
            ->order('update','desc')
            ->limit(5)->select();
        return $bookRecommend;
    }

//    站点推荐
    public function siteRecommend(){
        $siteRecommend = db("book")
            ->order('id','desc')
            ->where('state',1)
            ->limit(3)->select();
        return $siteRecommend;
    }
//    作者书籍
    public function authorBook($book){
        $authorBook  = db("book")
            ->where('m_id',$book['m_id'])
            ->where('state',1)
            ->order('id','desc')
            ->limit(4)->select();
        return $authorBook;
    }
    // 书籍目录
    public function directory(){
        $book_id = input("book_id");
        $book = db("book")->where('state',1)->where('id',$book_id)->find();
        $freeChapter = db("chapter")->where('book_id',$book_id)->where('is_free',0)->where('state',1)->select();
        $vipChapter = db("chapter")->where('book_id',$book_id)->where('is_free',1)->where('state',1)->select();
//        dump($book);die;
        $footer = $this->footer();
        $this->assign([
            'book' => $book,
            'freeChapter' => $freeChapter,
            'vipChapter' => $vipChapter,
            'footer' => $footer
        ]);
        return view("directory");
    }
    public function footer(){
        $data = db("web") ->cache(true,600)->find();
        return $data;
    }
    //计算书籍点击量
    public function book_pv($book_id,$book)
    {
        $ip = request()->ip();
        $key = $ip.':'.$book_id;
        $redis = new Redis();
        $val = $redis->get($key);
        if(!$val){
            $redis->set($key,$book_id,300);
            db('book')->where('id',$book_id)->update(['book_pv'=>$book['book_pv']+1]);
        }
        /*$book_pv = db('pv_log')->where('ip',$ip)->where('book_id',$book_id)->find();
        $time = time();
        if(empty($book_pv)){
            db('book')->where('id',$book_id)->update(['book_pv'=>$book['book_pv']+1]);
            db('pv_log')->insert(['ip' => $ip,'book_id'=>$book_id,'up_time' => time(),'chapter_id' => 0]);
        }else{
            if($time - $book_pv['up_time'] > 60){
                db('pv_log')->where('ip',$ip)->where('book_id',$book_id)->update(['up_time' => time(),'pv' => $book_pv['pv']+1]);
                db('book')->where('id',$book_id)->update(['book_pv'=>$book['book_pv']+1]);
            }
        }*/

    }
}